﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;

namespace Windvoice.Blog
{
    public partial class Comment : ActiveRecordBase<Comment>
    {
        public static int GetCommentCount(int articleID)
        {
            var query = from c in FindAll() where c.ArticleID == articleID select c;
            return query.Count();
        }

        public static Comment[] GetTopList(int count)
        {
            var query = from c in FindAll() orderby c.CreateTime descending select c;
            return query.Take(count).ToArray();
        }

        public static Comment[] GetList(int articleID)
        {
            var query = from c in FindAll() where c.ArticleID == articleID orderby c.CreateTime ascending select c;
            return query.ToArray();
        }

        public static Comment[] GetList(int pageIndex, int pageSize, int articleID, string content, out int recordCount)
        {
            var query = from c in FindAll() select c;
            var count = from c in FindAll() select c;

            if (!String.IsNullOrEmpty(content)){
                query = from c in query where c.Content.Contains(content) select c;
                count = from c in count where c.Content.Contains(content) select c;
            }
            if (articleID != 0){
                query = from c in query where c.ArticleID == articleID select c;
                count = from c in count where c.ArticleID == articleID select c;
            }

            recordCount = count.Count();
            return query.OrderByDescending(c=>c.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
        }
    }
}
